package com.wanxi.step07test.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanxi.step07test.dto.ResultDTO;
import com.wanxi.step07test.mapper.NewsMapper;
import com.wanxi.step07test.model.BusinessLineModel;
import com.wanxi.step07test.model.NewsModel;
import com.wanxi.step07test.model.NewsTypeModel;
import com.wanxi.step07test.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public ResultDTO add(NewsModel newsModel) {
        LambdaQueryWrapper<NewsModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsModel::getId)
                .eq(newsModel.getName()!=null,NewsModel::getName,newsModel.getName());
        boolean exists = newsMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("same name")
                    .count(-1)
                    .data("")
                    .build();
        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(newsMapper.insert(newsModel))
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO update(NewsModel newsModel) {
        LambdaQueryWrapper<NewsModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsModel::getId)
                .eq(newsModel.getName()!=null,NewsModel::getName,newsModel.getName());
        boolean exists = newsMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(newsMapper.updateById(newsModel))
                    .data("")
                    .build();

        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("same name")
                    .count(-1)
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO delete(NewsModel newsModel) {
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count(newsMapper.deleteById(newsModel))
                .data("")
                .build();
    }

    @Override
    public ResultDTO findById(NewsModel newsModel) {
        LambdaQueryWrapper<NewsModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsModel::getId,NewsModel::getName,NewsModel::getContent,
                        NewsModel::getImgHref,NewsModel::getIsRecommend,NewsModel::getIsShow,
                        NewsModel::getNewsTypeId,NewsModel::getTime,NewsModel::getEnable)
                .eq(newsModel.getId()!=null,NewsModel::getId,newsModel.getId());
        NewsModel news = newsMapper.selectOne(queryWrapper);
        return ResultDTO.builder()
                .code(0)
                .msg("")
//                .count()
                .data(news)
                .build();
    }

    @Override
    public ResultDTO findAll(NewsModel newsModel) {
        MPJLambdaWrapper<NewsModel> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(NewsModel::getId, NewsModel::getName, NewsModel::getImgHref,
                        NewsModel::getContent, NewsModel::getNewsTypeId, NewsModel::getEnable,
                        NewsModel::getIsRecommend, NewsModel::getIsShow, NewsModel::getTime)
                .selectAs(NewsTypeModel::getName,NewsModel::getNewsTypeName)
                .ge(newsModel.getStartTime() != null && newsModel.getStartTime() != "", NewsModel::getTime, newsModel.getStartTime())
                .le(newsModel.getEndTime() != null && newsModel.getEndTime() != "", NewsModel::getTime, newsModel.getEndTime())
                .like(newsModel.getName() != null && newsModel.getName() != "", NewsModel::getName, newsModel.getName())
                .like(newsModel.getContent() != null && newsModel.getContent() != "", NewsModel::getContent, newsModel.getContent())
                .eq(newsModel.getEnable()!=null && newsModel.getEnable() != -1,NewsModel::getEnable,newsModel.getEnable())
                .eq(newsModel.getIsRecommend()!=null && newsModel.getIsRecommend() != -1,NewsModel::getIsRecommend,newsModel.getIsRecommend())
                .eq(newsModel.getIsShow()!=null && newsModel.getIsShow() != -1,NewsModel::getIsShow,newsModel.getIsShow())
                .eq(newsModel.getNewsTypeId() != null && newsModel.getNewsTypeId() != 0, NewsModel::getNewsTypeId, newsModel.getNewsTypeId())
                .leftJoin(NewsTypeModel.class,NewsTypeModel::getId,NewsModel::getNewsTypeId);

        IPage<NewsModel> page= new Page<>(newsModel.getPage(),newsModel.getLimit());
        IPage<NewsModel> result = newsMapper.selectPage(page, wrapper);


        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count((int) result.getTotal())
                .data(result.getRecords())
                .build();
    }

}